﻿using ECMS.API.Controllers;
using ECMS.AppService.Interfaces;
using ECMS.AppService.Model.Requests;
using ECMS.AppService.Model.Responses;
using ECMS.Infrastructure.ClientData;
using ECMS.Infrastructure.Helper;
using ECMS.Infrastructure.Messaging;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace ECMS.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ForumController : BaseController
    {
        private readonly IPostService _postServeice;
        private readonly IReplyService _replyServeice;
        private readonly ILikeService _likeServeice;
        private readonly IViewService _viewServeice;
        private readonly IAppUserService _userService;
        public ForumController(IPostService postServeice, IReplyService replyServeice
            , ILikeService likeServeice, IViewService viewServeice, IAppUserService userService)
        {
            _postServeice = postServeice;
            _replyServeice = replyServeice;
            _likeServeice = likeServeice;
            _viewServeice = viewServeice;
            _userService = userService;
        }
        /// <summary>
        /// 发帖
        /// </summary>
        [HttpPost("Posting")]
        public async Task<ApiResult<SaveResponse>> Posting([FromBody]PostRequest model)
        {
            var response = new ApiResult<SaveResponse>();

            try
            {
                if (model == null || string.IsNullOrWhiteSpace(model.Title))
                {
                    return new ApiResult<SaveResponse> { Message = "参数无效或标题为空", StatusCode = -1, Success = false };
                }
                if (model.AppUserId <= 0)
                {
                    return new ApiResult<SaveResponse> { Message = "请重新登录", StatusCode = -2, Success = false };
                }
                response = await _postServeice.PostingAsync(model);
            }
            catch (Exception e)
            {
                new Logger("发帖失败").Error(e.Message);
            }
            return response;
        }

        /// <summary>
        /// 查看用户所有帖子
        /// </summary>
        [HttpPost("AllPostByUser")]
        public async Task<ApiResult<List<PostResponse>>> AllPostPageByUserAsync([FromBody]GetPostPagesRequest request)
        {
            var page = await _postServeice.AllPostPageByUserAsync(request);

            return null;
        }
        /// <summary>
        /// 推荐帖子
        /// </summary>
        [HttpPost("AllPost")]
        public async Task<dynamic> AllPostPageAsync([FromBody]GetPostPagesRequest request)
        {
            if (request == null)
            {
                return new { Message = "参数无效或标题为空", StatusCode = -1, Success = false };
            }

            var page = await _postServeice.AllPostPageAsync(request);

            if (page.Data.Items.Count > 0)
            {
                foreach (var item in page.Data.Items)
                {
                    //获取帖子点赞情况
                    var likeResponse = await _likeServeice.AllLikeByPostAsync(new LikeRequest { ForumId = item.Id });
                    var viewResponse = await _viewServeice.ViewPostCountAsync(new ViewRequest { ForumId = item.Id });
                    var replyResponse = await _replyServeice.AllReplyCountAsync(new ReplyRequest { ForumId = item.Id });
                    item.userResponse = await _userService.GetDetailAsync(item.AppUserId);
                    item.Hot = likeResponse.Data.Where(p => p.Status).Count();
                    item.IsLike = likeResponse.Data.Exists(p => p.AppUserId == request.AppUserId && p.Status);
                    item.View = viewResponse.Data.Count;
                    item.Replies = replyResponse.Data.Count;
                }
            }

            return page;
        }
        /// <summary>
        /// 查看帖子详情
        /// </summary>
        [HttpPost("PostDetail")]
        public async Task<ApiResult<PostResponse>> PostDetail([FromBody]GuidRequest request)
        {
            var response = new ApiResult<PostResponse>();
            if (request == null)
            {
                return new ApiResult<PostResponse> { Message = "参数无效" };
            }
            try
            {
                response = await _postServeice.PostDetailAsync(request);
                if (response.Data != null)
                {
                    var likeResponse = await _likeServeice.AllLikeByPostAsync(new LikeRequest { ForumId = response.Data.Id });
                    var viewResponse = await _viewServeice.ViewPostCountAsync(new ViewRequest { ForumId = response.Data.Id });
                    var replyResponse = await _replyServeice.AllReplyCountAsync(new ReplyRequest { ForumId = response.Data.Id });
                    response.Data.userResponse = await _userService.GetDetailAsync(response.Data.AppUserId);
                    response.Data.Hot = likeResponse.Data.Where(p => p.Status).Count();
                    response.Data.IsLike = likeResponse.Data.Where(p => p.AppUserId == response.Data.AppUserId).Select(p => p.Status).First();
                    response.Data.View = viewResponse.Data.Count;
                    response.Data.Replies = replyResponse.Data.Count;
                }
            }
            catch (Exception e)
            {
                new Logger("查看帖子详情发生错误").Error(e.Message);
            }
            return response;
        }
        /// <summary>
        /// 删除帖子
        /// </summary>
        [HttpPost("DeletePost")]
        public async Task<ApiResult<string>> DeletePost([FromBody]GuidRequest request)
        {
            var response = new ApiResult<string>();
            if (request == null)
            {
                return new ApiResult<string> { Message = "参数无效" };
            }
            try
            {
                response = await _postServeice.DeletePostAsync(request);
            }
            catch (Exception e)
            {
                new Logger("删除帖子发生错误").Error(e.Message);
            }
            return response;
        }
        /// <summary>
        ///  评论
        /// </summary>
        [HttpPost("Comment")]
        public async Task<ApiResult<SaveResponse>> Reply([FromBody]ReplyRequest model)
        {
            var response = new ApiResult<SaveResponse>();

            try
            {
                if (model == null || string.IsNullOrWhiteSpace(model.Content) || model.ForumId <= 0)
                {
                    return new ApiResult<SaveResponse> { Message = "参数无效", StatusCode = -1, Success = false };
                }
                if (model.AppUserId <= 0)
                {
                    return new ApiResult<SaveResponse> { Message = "请重新登录", StatusCode = -2, Success = false };
                }
                response = await _replyServeice.CommentAsync(model);
            }
            catch (Exception e)
            {
                new Logger("评论失败").Error(e.Message);
            }
            return response;
        }

        /// <summary>
        /// 查看评论
        /// </summary>
        [HttpPost("AllReply")]
        public async Task<dynamic> AllReplyPageAsync([FromBody]GetReplyPagesRequest request)
        {
            if (request == null)
            {
                return new { Message = "参数无效", StatusCode = -1, Success = false };
            }

            var page = await _replyServeice.AllReplyPageAsync(request);

            if (page.Data.Items.Count > 0)
            {
                foreach (var item in page.Data.Items)
                {
                    //获取帖子点赞情况
                    //var likeResponse = await _likeServeice.AllLikeByPostAsync(new LikeRequest { ForumId = item.Id });
                    //var viewResponse = await _viewServeice.ViewPostCountAsync(new ViewRequest { ForumId = item.Id });
                    //var replyResponse = await _replyServeice.AllReplyCountAsync(new ReplyRequest { ForumId = item.Id });
                    item.userResponse = await _userService.GetDetailAsync(item.AppUserId);
                    //item.Hot = likeResponse.Data.Where(p => p.Status).Count();
                    //item.IsLike = likeResponse.Data.Where(p => p.AppUserId == request.AppUserId).Select(p => p.Status).First();
                }
            }

            return page;
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        [HttpPost("DeleteReply")]
        public async Task<ApiResult<string>> DeleteReply([FromBody]GuidRequest request)
        {
            var response = new ApiResult<string>();
            if (request == null)
            {
                return new ApiResult<string> { Message = "参数无效" };
            }
            try
            {
                response = await _replyServeice.DeleteReplyAsync(request);
            }
            catch (Exception e)
            {
                new Logger("删除帖子发生错误").Error(e.Message);
            }
            return response;
        }


        /// <summary>
        /// 点赞
        /// </summary>
        [HttpPost("LikePost")]
        public async Task<ApiResult<SaveResponse>> LikePost(LikeRequest model)
        {
            var response = new ApiResult<SaveResponse>();
            if (model == null)
            {
                return new ApiResult<SaveResponse> { Message = "参数无效", StatusCode = 200, Success = false };
            }
            try
            {
                response = await _likeServeice.LikePostOrNotAsync(model);
            }
            catch (Exception e)
            {
                new Logger("发帖失败").Error(e.Message);
            }
            return response;
        }

    }
}